package databasetest;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

//준태 형 바보
//준태형 짱짱맨~ 은규가 ㅋㅋㅋ
public class DatabaseTest extends JFrame {
	
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";
	
	Connection conn; //connection 필드에뺌.
	DatabaseTest main;
	
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;
	
	Insert insert = null;
	Update update = null;
	Delete delete = null;
	Select select = null;
	
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DatabaseTest() {
		setTitle("데이터베이스연결");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 1200, 800);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		//데이터 베이스 연결테스트
		// 1) 드라이버 로딩
		try {
			Class.forName(driver).newInstance();
			System.out.println("드라이버가 로딩되었습니다.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		// 2) Connection 객체생성
		try {
			conn = DriverManager.getConnection(url, db_user, db_pwd);
			System.out.println("데이터베이스가 정상적으로 연결됨");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
	}
	
	public void openInsert(){  //기존insert의 내용을 따로 메서드로 분리함.
		if(insert==null){
			insert = new Insert(conn,this);  //여기서 this는 DatabaseTest자기자신..
			contentPane.add(insert);
		}
		insert.setVisible(true);
		insert.toFront();
	}
	
	public void openUpdate(){ //select 에서 선택한 행을 누르면 텍스트필드에 뿌려주는 역할..
		if(update==null){
			update = new Update(conn,this);
			contentPane.add(update);
		}
		update.setVisible(true);
		update.toFront(); //Window를 앞으로 가져옴.
	}


	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("New menu");
			mnNewMenu.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("입력");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
//					if(insert == null){
//						insert = new Insert(conn);  //  conn 매개변수 전달.!!! 
//						insert.setBounds(0, 0, 300, 350);
//						contentPane.add(insert);
//					}
//					insert.setVisible(true);
//					insert.toFront();  // 가장앞에뜨게.. 안해도됨..
					
					openInsert();  //위의 내용들을 담아 openInsert() 메서드로 따로 뺌.
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("수정");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
//					if(update == null){
//						update = new Update(conn,DatabaseTest.this);
//						update.setBounds(300, 0, 300, 350);
//						contentPane.add(update);
//					}
//					update.setVisible(true);	
					
					openUpdate();
					
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("삭제");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(delete == null){
						delete = new Delete(conn,DatabaseTest.this);
						delete.setBounds(0, 400, 300, 350);
						contentPane.add(delete);
					}
					delete.setVisible(true);						
				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("조회");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(select == null){
						select = new Select(conn,DatabaseTest.this);
						select.setBounds(300, 400, 300, 350);
						contentPane.add(select);
					}
					select.setVisible(true);	
				}
			});
		}
		return mntmNewMenuItem_3;
	}
}
